package cn.gdut.chengcai.admin.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.gdut.chengcai.base.ConstantUtil;
import cn.gdut.chengcai.base.FeedbackUtil;
import cn.gdut.chengcai.base.IdGenerator;
import cn.gdut.chengcai.base.JsonUtil;
import cn.gdut.chengcai.base.MessageUtil;
import cn.gdut.chengcai.base.Pager;
import cn.gdut.chengcai.base.PagerUtil;
import cn.gdut.chengcai.base.PartnerUtil;
import cn.gdut.chengcai.base.RequestUtil;
import cn.gdut.chengcai.base.ResponseData;
import cn.gdut.chengcai.base.UploadDownloadUtil;
import cn.gdut.chengcai.entity.ChengcaiInfo;
import cn.gdut.chengcai.entity.FeedbackInfo;
import cn.gdut.chengcai.entity.FeedbackInfoExample;
import cn.gdut.chengcai.entity.PartnerInfo;
import cn.gdut.chengcai.entity.PartnerInfoExample;
import cn.gdut.chengcai.entity.UploadInfoEntity;
import cn.gdut.chengcai.enums.UploadFileType;
import cn.gdut.chengcai.service.ChengcaiInfoService;
import cn.gdut.chengcai.service.EmailTemplateInfoService;
import cn.gdut.chengcai.service.FeedbackInfoService;
import cn.gdut.chengcai.service.KeyValueInfoService;
import cn.gdut.chengcai.service.PartnerInfoService;
import cn.gdut.chengcai.vo.FeedbackInfoVo;
import cn.gdut.chengcai.vo.PartnerInfoVo;

@Controller
@RequestMapping("/admin/about")
public class Admin_AboutController {

	@Autowired
	protected ChengcaiInfoService chengcaiInfoService;

	@Autowired
	protected FeedbackInfoService feedbackInfoService;

	@Autowired
	protected PartnerInfoService partnerInfoService;

	@Autowired
	protected EmailTemplateInfoService emailTemplateInfoService;

	@Autowired
	protected KeyValueInfoService keyValueInfoService;

	/**
	 * 保存成功页
	 */
	@RequestMapping("/saveSuccess")
	public String saveSuccess(HttpSession session, ModelMap map) {
		return "/admin/about/saveSuccess";
	}

	/**
	 * 企业介绍页
	 */
	@RequestMapping("/introduce")
	public String introduce(ModelMap map) {
		ChengcaiInfo entity = chengcaiInfoService.selectByPrimaryKey(1L);
		map.put("entity", entity);
		return "/admin/about/introduce";
	}

	/**
	 * 企业介绍修改操作
	 */
	@RequestMapping(value = "/modifyIntroduceAction", method = RequestMethod.POST)
	public String introduceModifyAction(HttpServletRequest request, HttpSession session) {
		ChengcaiInfo entity = chengcaiInfoService.selectByPrimaryKey(1L);
		entity.setProfile(RequestUtil.getParameter(request, "profile"));
		entity.setInstituteAbstract(RequestUtil.getParameter(request, "instituteAbstract"));
		entity.setInstituteCulture(RequestUtil.getParameter(request, "instituteCulture"));
		chengcaiInfoService.updateByPrimaryKey(entity);
		session.setAttribute(ConstantUtil.SESSION_RETURN_URL, "/admin/about/introduce.do");

		return "redirect:/admin/about/saveSuccess.do";
	}

	/**
	 * 联系我们页
	 */
	@RequestMapping("/contact")
	public String contact(ModelMap map) {
		ChengcaiInfo entity = chengcaiInfoService.selectByPrimaryKey(1L);
		map.put("entity", entity);
		return "/admin/about/contact";
	}

	/**
	 * 联系地址修改操作
	 */
	@RequestMapping(value = "/modifyContactAction", method = RequestMethod.POST)
	public String contactModifyAction(HttpServletRequest request, HttpSession session) {
		ChengcaiInfo entity = chengcaiInfoService.selectByPrimaryKey(1L);
		entity.setPhone(RequestUtil.getParameter(request, "phone"));
		entity.setEmail(RequestUtil.getParameter(request, "email"));
		entity.setLocation(RequestUtil.getParameter(request, "location"));
		chengcaiInfoService.updateByPrimaryKey(entity);
		session.setAttribute(ConstantUtil.SESSION_RETURN_URL, "/admin/about/contact.do");

		return "redirect:/admin/about/saveSuccess.do";
	}

	/**
	 * 意见反馈列表页
	 */
	@RequestMapping("/feedback/list")
	public String feedbackList(HttpSession session, ModelMap map) {
		FeedbackInfoExample example = new FeedbackInfoExample();
		example.createCriteria().andIsDeletedEqualTo(false);
		List<FeedbackInfo> listTotal = feedbackInfoService.selectByExample(example);
		Long itemCount = Long.valueOf(String.valueOf(listTotal.size()));
		Pager pager = PagerUtil.getPager(session, itemCount);
		int fromIndex = (pager.getPageNo() - 1) * pager.getPageSize();
		if (fromIndex >= listTotal.size()) {
			fromIndex = 0;
			pager.setPageNo(1);
			session.setAttribute(ConstantUtil.STR_PAGE_NO, "1");
		}
		int toIndex = fromIndex + pager.getPageSize();
		if (toIndex >= listTotal.size()) {
			toIndex = listTotal.size();
		}
		List<FeedbackInfo> list = listTotal.subList(fromIndex, toIndex);
		try {
			if ((null != list) && (list.size() > 0)) {
				List<FeedbackInfoVo> voList = new ArrayList<>();
				FeedbackUtil.fillVoList(list, voList);
				map.put("list", voList);
				session.setAttribute("pager", pager);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/admin/about/feedback/list";
	}

	/**
	 * 意见反馈详情页
	 */
	@RequestMapping("/feedback/detail/{id}")
	public String feedbackDetail(ModelMap map, @PathVariable Long id) {
		FeedbackInfo entity = feedbackInfoService.selectByPrimaryKey(id);
		FeedbackInfoVo vo = new FeedbackInfoVo();
		try {
			FeedbackUtil.fillVo(entity, vo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		map.put("entity", vo);
		return "/admin/about/feedback/detail";
	}

	/**
	 * 意见反馈处理页
	 */
	@RequestMapping("/feedback/handle/{id}")
	public String feedbackHandle(ModelMap map, @PathVariable Long id) {
		FeedbackInfo entity = feedbackInfoService.selectByPrimaryKey(id);
		FeedbackInfoVo vo = new FeedbackInfoVo();
		try {
			FeedbackUtil.fillVo(entity, vo);
			map.put("entity", vo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/admin/about/feedback/handle";
	}

	/**
	 * 处理意见反馈操作
	 */
	@RequestMapping(value = "/feedback/handleAction", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData<Object> feedbackHandleAction(@RequestBody String jsonString, HttpServletRequest request,
			HttpSession session) {
		ResponseData<Object> result = new ResponseData<>();
		result.setIsSuccess(false);
		try {
			FeedbackInfo param = (FeedbackInfo) JsonUtil.fromJson(jsonString, FeedbackInfo.class);
			FeedbackInfo entity = feedbackInfoService.selectByPrimaryKey(param.getId());
			entity.setReplyOperator(param.getReplyOperator());
			entity.setReplyOpinion(param.getReplyOpinion());
			entity.setReplyResult(param.getReplyResult());
			entity.setIsReply(true);
			entity.setReplyTime(new Date());
			feedbackInfoService.updateByPrimaryKey(entity);
			result.setIsSuccess(true);
		} catch (Exception e) {
			result.setMessage(MessageUtil.SYSTEM_ERROR);
			// log.error(e.getMessage(), e);
		}
		return result;
	}

	/**
	 * 删除意见反馈操作
	 */
	@RequestMapping(value = "/{id}/deleteFeedbackAction", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData<Object> feedbackDeleteAction(@RequestBody String jsonString, @PathVariable Long id) {
		ResponseData<Object> result = new ResponseData<>();
		result.setIsSuccess(false);
		FeedbackInfo entity = feedbackInfoService.selectByPrimaryKey(id);
		try {
			// feedbackInfoService.deleteById(entity.getId());
			// 逻辑删除
			entity.setIsDeleted(true);
			feedbackInfoService.updateByPrimaryKey(entity);
			result.setIsSuccess(true);
		} catch (Exception e) {
			result.setMessage(MessageUtil.SYSTEM_ERROR);
			// log.error(e.getMessage(), e);
		}
		return result;
	}

	/**
	 * 合作伙伴列表页
	 */
	@RequestMapping("/partner/list")
	public String partnerList(HttpSession session, ModelMap map) {
		PartnerInfoExample example = new PartnerInfoExample();
		example.createCriteria().andIsDeletedEqualTo(false);
		List<PartnerInfo> listTotal = partnerInfoService.selectByExample(example);
		Long itemCount = Long.valueOf(String.valueOf(listTotal.size()));
		Pager pager = PagerUtil.getPager(session, itemCount);
		int fromIndex = (pager.getPageNo() - 1) * pager.getPageSize();
		if (fromIndex >= listTotal.size()) {
			fromIndex = 0;
			pager.setPageNo(1);
			session.setAttribute(ConstantUtil.STR_PAGE_NO, "1");
		}
		int toIndex = fromIndex + pager.getPageSize();
		if (toIndex >= listTotal.size()) {
			toIndex = listTotal.size();
		}
		List<PartnerInfo> list = listTotal.subList(fromIndex, toIndex);
		try {
			if ((null != list) && (list.size() > 0)) {
				List<PartnerInfoVo> voList = new ArrayList<>();
				PartnerUtil.fillVoList(list, voList);
				map.put("list", voList);
				session.setAttribute("pager", pager);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/admin/about/partner/list";
	}

	/**
	 * 合作伙伴添加页
	 */
	@RequestMapping("/partner/addnew")
	public String partnerAddnew(ModelMap map) {
		return "/admin/about/partner/addnew";
	}

	/**
	 * 合作伙伴新增操作
	 */

	@RequestMapping("/partner/addnewAction")
	public String partnerAddnewAction(HttpServletRequest request, HttpSession session) {

		PartnerInfo partnerInfo = new PartnerInfo();
		partnerInfo.setSn(IdGenerator.generatorPartnerSn());
		partnerInfo.setName(RequestUtil.getParameter(request, "name"));
		partnerInfo.setHomepage(RequestUtil.getParameter(request, "homepage"));
		partnerInfo.setLinkman(RequestUtil.getParameter(request, "linkman"));
		partnerInfo.setContactNumber(RequestUtil.getParameter(request, "contactNumber"));
		partnerInfo.setContactEmail(RequestUtil.getParameter(request, "contactEmail"));
		partnerInfo.setIntro(RequestUtil.getParameter(request, "intro"));
		partnerInfo.setIsShow(Boolean.valueOf(RequestUtil.getParameter(request, "isShow")));
		partnerInfo.setMemo(RequestUtil.getParameter(request, "memo"));
		partnerInfo.setIsDeleted(false);
		partnerInfo.setCreateTime(new Date());

		// 上传处理
		Map<Integer, UploadInfoEntity> map = UploadDownloadUtil.uploadAction(request, session,
				UploadFileType.PROFILE.getValue(), partnerInfo.getCreateTime());
		if (null != map && map.size() != 0) {
			String filename = "";
			if (null != map.get(1)) {
				filename = partnerInfo.getLogo();
				if (StringUtils.isEmpty(filename) == false) {
					UploadDownloadUtil.deleteAction(filename);
				}
				partnerInfo.setLogo(map.get(1).getFileUrl());
			}
		}

		partnerInfoService.insert(partnerInfo);
		return "redirect:/admin/about/partner/list.do";
	}


	/**
	 * 合作伙伴详情页
	 */
	@RequestMapping("/partner/detail/{id}")
	public String partnerDetail(ModelMap map, @PathVariable Long id) {
		PartnerInfo entity = partnerInfoService.selectByPrimaryKey(id);
		PartnerInfoVo vo = new PartnerInfoVo();
		try {
			PartnerUtil.fillVo(entity, vo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		map.put("entity", vo);
		return "/admin/about/partner/detail";
	}
	
	/**
	 * 删除合作伙伴操作
	 */
	@RequestMapping(value = "/{id}/deletePartnerAction", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData<Object> partnerDeleteAction(@RequestBody String jsonString, @PathVariable Long id) {
		ResponseData<Object> result = new ResponseData<>();
		result.setIsSuccess(false);
		PartnerInfo entity = partnerInfoService.selectByPrimaryKey(id);
		try {
			// 逻辑删除
			entity.setIsDeleted(true);
			partnerInfoService.updateByPrimaryKey(entity);
			result.setIsSuccess(true);
		} catch (Exception e) {
			result.setMessage(MessageUtil.SYSTEM_ERROR);
			// log.error(e.getMessage(), e);
		}
		return result;
	}

}